package akbderby.service;

import java.util.ArrayList;
import java.util.List;

import org.slim3.datastore.Datastore;

import akbderby.meta.QuizMeta;
import akbderby.model.Quiz;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class QuizService extends ServiceBase {

    private QuizMeta t = QuizMeta.get();

    public Quiz getQuiz(String id) {
        Key quizKey = KeyFactory.stringToKey(id);
        return getQuiz(quizKey);
    }

    public Quiz getQuiz(Key quizKey) {
        if (isNull(quizKey))
            return null;
        return Datastore.get(Quiz.class, quizKey);
    }

    public List<Quiz> getSelect(String q, int limit) {

        List<Quiz> l =
            Datastore.query(t).sort(t.createDate.desc).limit(limit).asList();

        if (isNull(q)) {
            return l;
        }

        List<Quiz> returnList = new ArrayList<Quiz>();

        for (Quiz data : l) {
            if (data.getSearchWord().indexOf(q) != -1) {
                returnList.add(data);
            }
        }
        return returnList;
    }
}
